import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.JOptionPane;

import player.PlayerFrame;

/**
 * Created by Dominik on 2014-10-18.
 * Changed by Agnieszka on 2014-10-19.
 */
public class Player extends Thread{
	
    public static int PLAYER_IN_GAME = 0;
    public static int PLAYER_OUT_OF_GAME = -1;
    public static int PLAYER_PLAYED_ALLIN = 1;
    
	private String name;
	private Game game;
    private int number;
    private int playersCoins;
    private int bet = 0;
    private int status = PLAYER_IN_GAME;
    private Hand PlayerHand;
    private Socket client;
    boolean works;
    BufferedReader in = null;
	PrintWriter out = null;

    
	public Player(int number,Socket client, int startingCoins, Game game,Deck deck){
  
		this.game = game;
        this.number = number;
        this.playersCoins = startingCoins;
        this.PlayerHand = new Hand(deck, this);
        this.client = client;
		this.works = true;
        
        try{
			in = new BufferedReader(new InputStreamReader(client.getInputStream()));
			out = new PrintWriter(client.getOutputStream(), true);
		} 
		catch (IOException e){
			System.out.println("in or out failed");
			System.exit(-1);
		}
    }
    
    public String getNick() {
        return name;
    }
    public int getNumber() {
        return number;
    }
    public int getBet(){
    	return bet;
    }
    public int getPlayersCoins() {
        return playersCoins;
    }
    public Hand getPlayerHand() {
        return PlayerHand;
    }
    public void exchangeCards(Deck deck, int cardSet){
    	PlayerHand.changeCards(deck, number, cardSet);
    }
    public void setBet(int bet){
    	this.bet = bet;
    }
    public void setStatus(int status){
    	this.status = status;
    }
    public int getStatus(){
    	return status;
    }
    public boolean isWorking(){
    	return works;
    }
    public void setPlayersCoins(int coins) {
        this.playersCoins = coins;
    }
    public void addNewPlayer(String name){
    	out.println("addNewPlayer" + name);
    }
    public void displayMessage(String message){
    	out.println("Chat_" + message);
    }
    public void displayWinner(String message) {
        try {
            sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        out.println("Winner_" + message);
    }
    public void displayHand(){
    	out.println("Hand_" + PlayerHand.getString() );
    }
    public void displayBigBlind(String name, String bBlind){
    	out.println("BigBlind_" + name + bBlind);
    }
    public void displaySmallBlind(String name,String sBlind){
    	out.println("SmallBlind_" + name + sBlind);
    }
    public void displayCoins(String amount, String name){
    	out.println("sCoins_"+ name + amount);
    }
    public void displayBet(String bet, String name){
    	out.println("Bet_" + name + bet);
    }
    public void enableButtons(String buttons){
    	out.println("Enable_"+buttons);
    }
    public void displayFold(String name){
    	out.println("dFold_"+ name);
    }
    public void displayAllin(String name){
    	out.println("dAllin_"+ name);
    }
    public void displayStake(String stake){
    	out.println("Stake_" + stake);
    }
    public void displayRound(String round){
    	out.println("Round_" + round);
    }
    public void displayCards(){
        out.println("Cards_" + PlayerHand.getString());
    }
    public void displayAmountOfExchanged(String name,String amount){
    	out.println("Amount_"+ name + amount);
    }
    public void deletePlayer(String name){
    	out.println("Delete_" + name);
    }
    
    
    
	public void run(){
		String line = "";
		while(line !=null && works){
			try{
				line = in.readLine();
				System.out.println("Player " + name + " get line: " + line);
				if (line.startsWith("Name_")){
                    this.name = line.substring(5);
                    out.println("sCoins_" +name + playersCoins);
                    out.println("previousPlayers_" + game.introduceNewPlayer(name));
				}
				else if (line.startsWith("Send_")){
					game.displayMessage(line.substring(5));
				}
				else if (line.startsWith("Bet_")){
					game.updateBet(number,line.substring(4));
				}
				else if (line.startsWith("Check_")){
					game.nextPlayerMove();
				}
				else if (line.startsWith("All_")){
					game.updateAllin(number);
				}
				else if (line.startsWith("Fold_")){
					game.updateFold(number);
				}
				else if (line.startsWith("Exchange_")){
					game.exchangeCards(number,line.substring(9));
				}
				else if (line.startsWith("Close_")){
					game.closeResults(number);
				}
				
			}
			catch (IOException e){
				System.out.println("Player Read failed.");
				works = false;
				game.updateFold(number);
				//gracz wyrzucany z pczatkiem nastepnego rozegrania
			}
		}
	}
}
